﻿$(function () {
 Show()
    
})
	    //显示复检信息
	    function Show() {
    	        layui.use(function () {
                    var table = layui.table;
                    // 点击导出按钮
                    document.getElementById('exportBtnRe').addEventListener('click', function () {
                        var checkStatus = table.checkStatus('Retable');  // 获取当前表格的选中状态
                        var selecteIds = checkStatus.data;  // 获取选中的数据
                        if (selecteIds.length === 0) {
                            alert('请先选择要导出的数据!');
                            return;
                        }
                        var filteredData = selecteIds.map(function (item) {
                            // 创建一个新对象，只包含您想要的属性
                            return {
                                '复检编号': item.reinspectionCode,
                                '复检名称': item.reinspectionName,
                                '复检状态': item.reinspectionSarte,
                                '合格状态': item.heGeSarte,
                                '复检日期': item.reinspectionTime,
                                '复检人': item.reName,
                                '备注': item.reinspectionNote,

                            };
                        });

                        // 使用 XLSX 导出选中的数据
                        var wb = XLSX.utils.book_new();
                        var ws = XLSX.utils.json_to_sheet(filteredData); // 将选中的数据转换为工作表
                        XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

                        // 导出为 Excel 文件
                        XLSX.writeFile(wb, '复检列表.xlsx');
                    });
        	           
       
                    var ReinspectionCode = $("#ReInCode").val();
                    var ReinspectionName = $("#ReInName").val();
                    var ReinspectionSarte = $("#ReInSarte").val();
       
               // 先进行表格的初始渲染
               table.render({
                   elem: '#Retable',  // 表格元素的选择器
                       page: {
                           limit: 5,
                           limits: [5, 8, 10]

        },
                      cols: [[
                              { type: 'checkbox', fixed: 'left' },
                          { field: 'reinspectionCode', title: '复检编号' },
                          { field: 'reinspectionName', title: '复检名称' },
                             
                          { field: 'reinspectionSarte', title: '复检状态' },
                          { field: 'heGeSarte', title: '合格状态' },
                              {
                                  field: 'reinspectionTime', title: '复检日期',
            	                        templet: function(d) {
                                                  // 假设createdDate字段是一个日期字符串，格式为'YYYY-MM-DD HH:MM:SS'
                                                  // 截取前10位以获取日期部分.substring(0, 10)
                                            var dateStr = d.reinspectionTime;
                                                  // 返回包含日期的HTML字符串（这里可以简单地返回日期字符串，不需要额外的HTML标签）
                                                  return dateStr;
                        
                    }
	                    },
                          { field: 'reName', title: '复检人' },
                          { field: 'reinspectionNote', title: '备注' },
               	
	                    {
                            field: '', title: '操作', templet: function (d) {
                                if (d.reinspectionSarte === '待复检') {
                                    return '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" onclick="ReInDialog(' + d.reinspectionId + ')"><i class="layui-icon layui-icon-edit"></i>复检</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs "disabled onclick="return false;"><i class="layui-icon layui-icon-edit"></i>销毁（禁用）</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" disabled onclick="return false;"><i class="layui-icon layui-icon-edit"></i>查看(禁用)</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-danger layui-btn-xs "disabled onclick="return false;"><i class="layui-icon layui-icon-delete"></i>删除（禁用）</a>';
                                }
                                else if (d.heGeSarte === '已合格') {
                                    return '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" disabled onclick="return false;"><i class="layui-icon layui-icon-edit"></i>复检(禁用)</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs "disabled onclick="return false;"><i class="layui-icon layui-icon-edit"></i>销毁（禁用）</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" onclick="showData1(' + d.reinspectionId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.reinspectionId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                                }
                                else if(d.heGeSarte === '不合格') {
                                    return '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" disabled onclick="return false;"><i class="layui-icon layui-icon-edit"></i>复检(禁用)</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" onclick="editHui(\'' + d.reinspectionId + '\', \'' + d.rawMaterialQualityId + '\')"><i class="layui-icon layui-icon-edit"></i>销毁</a>'+

                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" onclick="showData2(' + d.reinspectionId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' +
                                        '<a href="javascript:void(0);" class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.reinspectionId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                                }


                            }
                }
]],
            	                // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
         data: []
     });

         // 创建表格实例
         $.ajax({
             url: "../Quality/ShowReIn",
                type: "get",
                data: {
                    Pageindex: 1,
                    Pagesize: 100,
                    token: localStorage.getItem("token"),
                    ReinspectionCode: ReinspectionCode,
                    ReinspectionName: ReinspectionName,
                    ReinspectionSarte: ReinspectionSarte
    },

    success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.list);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
        table.reload('Retable', {
                        page: {
                            limit: 5,
                            limits: [5, 8, 10]

        },
                                    data: data.data.list
                });

},
            error: function () {
                    console.log("请求失败");

}
	            });

	        });

	    }
	    // 单删数据的函数
window.deleteData = function (reinspectionId) {
     layer.confirm('确定要删除该条数据吗？', function () {
             $.ajax({
    	                type: "post",
                 url: "../Quality/IsDeleteReInfo",  // 替换为实际的后台删除数据接口地址
                 data: { ReinspectionId: reinspectionId, token: localStorage.getItem("token") },
    	                success: function (res) {
                        console.log(res)
                        var res = JSON.parse(res)
                        if (res.code == 1) {
    	                        layer.alert("删除成功");
                             Show();  // 重新加载数据，刷新表格显示
                    
                } else {
                   layer.alert("删除失败");
                   
                   
                }
                
            },
                   error: function () {
                           layer.msg('删除失败，请稍后重试', {
                                   time: 2000,
                                   icon: 2

                });
                
            }
        });


});
	    };
	    //已合格查看跳转
window.showData1 = function (reinspectionId) {
    localStorage['reinspectionId'] = reinspectionId;
    
        //localStorage.setItem("purchaseID", purchaseID)
        //console.log(localStorage.getItem("purchaseID"))
    location.href = '/Quality/HegeReInShow';
    
}
//不合格查看跳转
window.showData2 = function (reinspectionId) {
    localStorage['reinspectionId'] = reinspectionId;

    //localStorage.setItem("purchaseID", purchaseID)
    //console.log(localStorage.getItem("purchaseID"))
    location.href = '/Quality/BuHegeReInfIndexShow';

}
//销毁
var obj1 = {
    ReinspectionId: "",
    RawMaterialQualityId: ""
}
window.editHui = function editHui(reinspectionId, rawMaterialQualityId) {
    obj1.ReinspectionId = reinspectionId
    obj1.RawMaterialQualityId = rawMaterialQualityId
    $.ajax({
        type: "post",
        url: "../Quality/XiaohuiRein",  // 替换为实际的后台删除数据接口地址
        data: { obj1: obj1, token: localStorage.getItem("token") },
        success: function (res) {
            console.log(res)
            var res = JSON.parse(res)
            if (res.code == 1) {
                layer.alert("销毁成功");
                Show()

            } else {
                layer.alert("销毁失败");


            }

        },
        error: function () {
            layer.msg('删除失败，请稍后重试', {
                time: 2000,
                icon: 2

            });

        }
    });
}
//复检跳转
window.ReInDialog = function (reinspectionId) {
    localStorage['reinspectionId'] = reinspectionId;

    //localStorage.setItem("purchaseID", purchaseID)
    //console.log(localStorage.getItem("purchaseID"))
    location.href = '/Quality/ReInfICreate';

}

  